//
//  MessageContentButton.m
//  ChatDemo
//
//  Created by 陈石 on 15/5/13.
//  Copyright (c) 2015年 陈石. All rights reserved.
//

#import "MessageContentButton.h"

@implementation MessageContentButton

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        //图片
        self.backImageView                        = [[UIImageView alloc] init];
        self.backImageView.userInteractionEnabled = YES;
        self.backImageView.layer.cornerRadius     = 5;
        self.backImageView.layer.masksToBounds    = YES;
        self.backImageView.contentMode            = UIViewContentModeScaleAspectFill;
        self.backImageView.backgroundColor        = [UIColor whiteColor];
        [self addSubview:self.backImageView];

        //语音
        self.voiceBackView = [[UIView alloc] init];
        [self addSubview:self.voiceBackView];
        self.second                     = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 70, 30)];
        self.second.textAlignment       = NSTextAlignmentCenter;
        self.second.font                = [UIFont systemFontOfSize:14];
        self.voice                      = [[UIImageView alloc] init];
        self.voice.animationDuration    = 1;
        self.voice.animationRepeatCount = 0;
        self.indicator                  = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
        self.indicator.center           = CGPointMake(80, 15);
        [self.voiceBackView addSubview:self.indicator];
        [self.voiceBackView addSubview:self.voice];
        [self.voiceBackView addSubview:self.second];

        self.backImageView.userInteractionEnabled = NO;
        self.voiceBackView.userInteractionEnabled = NO;
        self.second.userInteractionEnabled        = NO;
        self.voice.userInteractionEnabled         = NO;

        self.second.backgroundColor        = [UIColor clearColor];
        self.voice.backgroundColor         = [UIColor clearColor];
        self.voiceBackView.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (void)benginLoadVoice
{
    self.voice.hidden = YES;
    [self.indicator startAnimating];
}

- (void)didLoadVoice
{
    self.voice.hidden = NO;
    [self.indicator stopAnimating];
    [self.voice startAnimating];
}

- (void)stopPlay
{
    //    if(self.voice.isAnimating){
    [self.voice stopAnimating];
    //    }
}

- (void)setIsMyMessage:(BOOL)isMyMessage
{
    _isMyMessage = isMyMessage;
    if (isMyMessage)
    {
        self.backImageView.frame = CGRectMake(5, 5, 220, 220);
        self.voiceBackView.frame = CGRectMake(15, 10, 130, 35);
        self.second.textColor    = [UIColor whiteColor];
    }
    else
    {
        self.backImageView.frame = CGRectMake(15, 5, 220, 220);
        self.voiceBackView.frame = CGRectMake(25, 10, 130, 35);
        self.second.textColor    = [UIColor grayColor];
    }
}

//添加
- (BOOL)canBecomeFirstResponder
{
    return YES;
}

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    return (action == @selector(copy:));
}

- (void)copy:(id)sender
{
    UIPasteboard *pboard = [UIPasteboard generalPasteboard];
    pboard.string = self.titleLabel.text;
}

@end
